/**
 * Copyright (c) 2010 Jesco Freund.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of Backtory nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  $Id$
 */

#include <stdlib.h>
#include <inttypes.h>
#include "_threading.h"

/**
 * function pointer returning thread id
 * to be set by YAR_THREADING_threadid_set_callback()
 */
void (*__yar_threading_threadid_callback)(yar_threadid_t *id);


/**
 * function pointer to locking callback function
 * to be set by YAR_THREADING_locking_set_callback()
 */
void (*__yar_threading_locking_callback)(int mode, int n, const char *file, int line);


/**
 * sets function pointer to given callback function
 */
extern void YAR_THREADING_threadid_set_callback(void (*threadid_func)(yar_threadid_t *id))
{
	__yar_threading_threadid_callback = threadid_func;
}


/**
 * sets a numeric thread id
 */
extern void YAR_THREADING_threadid_set_numeric(yar_threadid_t *id, uint64_t val)
{
	id->ptr = NULL;
	id->val = val;
}


/**
 * sets a pointer thread id
 */
extern void YAR_THREADING_threadid_set_pointer(yar_threadid_t *id, void *ptr)
{
	id->ptr = ptr;
	id->val = 0;
}


/**
 * record the current thread id
 */
extern void YAR_THREADING_threadid_current(yar_threadid_t *id)
{
	(*__yar_threading_threadid_callback)(id);
}


/**
 * default callback function to deliver dummy thread id
 * this function is used as a dummy as long as threading
 * support has not been activated by the program using
 * this library.
 */
void __yar_threading_threadid_default_callback(yar_threadid_t *id)
{
	YAR_THREADING_threadid_set_numeric(id, 0);
}


extern int YAR_THREADING_locking_num_locks(void)
{
	return YAR_THREADING_NUMLOCKS;
}

/**
 * sets function pointer to given callback function
 */
extern void YAR_THREADING_locking_set_callback(void (*func)(int mode, int n, const char *file, int line))
{
	__yar_threading_locking_callback = func;
}


/**
 * set the given mode for mutex no. n.
 */
extern void YAR_THREADING_locking_set_mode(int mode, int n, const char *file, int line)
{
	(*__yar_threading_locking_callback)(mode, n, file, line);
}


/**
 * Default callback function to set/release lock.
 * This function does not do anything real, it is just a dummy!
 * For real thread safety, a working callback function has to be set.
 */
void __yar_threading_locking_default_callback(int mode, int n, const char *file, int line)
{
	return;
}


/**
 * standard module initializing function called by global YAR_init()
 */
void yar_threading_init(void)
{
	__yar_threading_threadid_callback = __yar_threading_threadid_default_callback;
	__yar_threading_locking_callback = __yar_threading_locking_default_callback;
}
